//
//  ToModelViewController.swift
//  XMenSwift
//
//  Created by Jason on 2023/5/26.
//

import UIKit

class TestModel: NSObject,Codable {

    var a: String?

    var b: String?
}

struct MovieModel: Codable {
    
    @Codec(transformer: TransformOf<TestModel?,String?>(fromJSON: { (value: Any?) -> TestModel? in
        if let jsonString = value as? String {
            return JSONTool.jsonToModel(TestModel.self, from: jsonString)
        }
        return nil
    }, toJSON: { value -> String? in
        return ""
//        return value?.toJSONString()
    }))    
    @Codec("text_T")
    var text: TestModel? = nil
    
    var movieDetail: MovieDetailModel? = nil

    @Codec var sex: Sex?
        

    private enum CodingKeys: String, CodingKey {
           case movieDetail = "movie_detail"
       }
}


/// rawValue为String的枚举
///
/// - man: 男
/// - woman: 女
/// - unknown: 未知
enum Sex: String, Codable {
    case man = "man"
    case woman = "woman"
    case unknown = "unknown"
}


class CommentsModel: Codable {
    
//    @Codec
    var myText: String?
    
    @Codec
    var user_name: String?

    @Codec
    var rating: String?
    
    var comment: String?
    
    @Codec("aString")
    var stringVal: String = ""
    
//    enum CodingKeys: CodingKey {
//        case myText
//        case user_name
//        case rating
//        case comment
//        case stringVal
//    }

}


struct MovieDetailModel: Codable {

    @Codec var max: [String: String]?
    
    @Codec var min: String?
    
    @Codec var title: String?
    
    @Codec var synopsis: [String]?
    
    @Codec var comments: [CommentsModel]?
    
    enum CodingKeys: CodingKey {
        case max
        case min
        case title
        case synopsis
        case comments
    }
}

class ToModelViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let dict: [String : Any] = [
             "sex": "woman",
             "test": ["a": "a",
                      "b": "b",
                      "c": "c"],
             "movie_detail":
                 [
                     "max": ["a": "3"],
                     "min": 4,
                     "title":"长津湖",
                     "synopsis":"《长津湖",
                     "theater":["A影院","B影院","C影院"],
                     "comments":
                         [
                             ["user_name":"user001","rating":5,"comment":"评论1！"],
                             ["user_name":"user002","rating":5,"comment":"评论2！"],
                             ["user_name":"user003","rating":4,"comment":"评论3！"]
                         ]
                 ]
         ]
        
        
        
        let model = JSONTool.jsonToModel(MovieModel.self, from: dict)
        let comments =
        [
            ["user_name":"user001","rating":5,"comment":"评论1！"],
            ["user_name":"user002","rating":5,"comment":"评论2！"],
            ["user_name":"user003","rating":4,"comment":"评论3！"]
        ]
        
        guard let modelArray = JSONTool.jsonArrayToModelArray(CommentsModel.self, from: comments) else { return}
        let modelArrayStr = JSONTool.modelArrayToString(modelArray)
        let dictA = JSONTool.modelArrayToDict(modelArray)
        
        print(modelArrayStr)
        
        
    }
    
    

}
